package org.feng.fxfront.pages.components;

import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import lombok.Data;
import org.feng.fxfront.util.ControlUtil;

/**
 * 自定义分页组件
 *
 * @author feng
 */
@Data
public class CustomPagingComponent {

    /**
     * 每页条数
     */
    private ComboBox<String> pageSize;

    /**
     * 当前页
     */
    private TextField currentPage;

    /**
     * 上一页
     */
    private Button lastPageButton;
    /**
     * 下一页
     */
    private Button nextPageButton;

    /**
     * 跳转
     */
    private Button skipPageButton;

    /**
     * 首页
     */
    private Button firstPageButton;
    /**
     * 最后一页
     */
    private Button finallyPageButton;

    /**
     * 分页面板
     */
    private GridPane pagingGridPane;

    /**
     * 总页数
     */
    private Label pageCount;

    public CustomPagingComponent(double width, double height) {
        this(width, height, null, null);
    }

    public CustomPagingComponent(double width, double height, Integer pageCount, Integer pageSize) {
        pagingGridPane = new GridPane();
        pagingGridPane.setMaxWidth(width);
        pagingGridPane.setMinWidth(width);
        pagingGridPane.setMaxHeight(height);
        pagingGridPane.setMinHeight(height);
        pagingGridPane.setPadding(new Insets(8, 0, 8, 0));

        // 设置下拉框，选择每页条数；默认15条
        this.pageSize = new ComboBox<>();
        this.pageSize.getItems().addAll("10", "15", "30", "50", "100");
        this.pageSize.setValue(pageSize == null ? "15" : String.valueOf(pageSize));

        // 当前页默认设置为1
        currentPage = new TextField();
        currentPage.setText("1");
        currentPage.setMaxWidth(35);
        // 上一页，下一页
        lastPageButton = ControlUtil.pagingButton("上一页");
        nextPageButton = ControlUtil.pagingButton("下一页");
        skipPageButton = ControlUtil.pagingButton("跳转");
        firstPageButton = ControlUtil.pagingButton("首页");
        finallyPageButton = ControlUtil.pagingButton("最后一页");

        // 总页数
        this.pageCount = new Label(String.format(" %s ", pageCount == null ? "0" : String.valueOf(pageCount)));

        // 设置垂直间距，水平间距
        pagingGridPane.setVgap(5);
        pagingGridPane.setHgap(3);

        pagingGridPane.add(new Label("总页数:"), 0, 0);
        pagingGridPane.add(this.pageCount, 1, 0);
        Node spacer1 = new Region();
        HBox.setHgrow(spacer1, Priority.ALWAYS);
        pagingGridPane.add(spacer1, 2, 0);

        pagingGridPane.add(firstPageButton, 3, 0);
        pagingGridPane.add(lastPageButton, 4, 0);
        pagingGridPane.add(currentPage, 5, 0);
        pagingGridPane.add(nextPageButton, 6, 0);
        pagingGridPane.add(finallyPageButton, 7, 0);
        pagingGridPane.add(skipPageButton, 8, 0);

        Node spacer2 = new Region();
        HBox.setHgrow(spacer2, Priority.ALWAYS);
        pagingGridPane.add(spacer2, 9, 0);
        pagingGridPane.add(this.pageSize, 10, 0);
    }

    public int getCurrentPageValue() {
        return Integer.parseInt(this.getCurrentPage().getText());
    }

    public void setCurrentPageValue(int currentPageValue) {
        this.getCurrentPage().setText(String.valueOf(currentPageValue));
    }

    public int getPageSizeValue() {
        return Integer.parseInt(this.getPageSize().getValue());
    }

    public void setPageSizeValue(int pageSizeValue) {
        this.getPageSize().setValue(String.valueOf(pageSizeValue));
    }

    public void setPageCountValue(int pageCountValue) {
        this.getPageCount().setText(String.valueOf(pageCountValue));
    }

    public int getPageCountValue() {
        return Integer.parseInt(this.getPageCount().getText());
    }
}
